<?php

namespace App\Http\Controllers;

use App\Models\Prize;
use App\Models\Prizelist;
use App\Models\Scorelist;
use App\Models\Scoreshop;
use App\Models\Sign;
use App\Models\Travelnote;
use App\Models\Wxuser;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Illuminate\Support\Facades\Input;

class ScoreController extends WxBaseController
{
    public function getSignDays(){
        $sign = Sign::where('wxuser_id',$this->auth()->id)->first();
        if($sign){
            if(is_today($sign->last_signin_time)){
                return $this->apiReturn(true,'success',['score'=>$sign->score,'days'=>$sign->days,'status'=>1]);
            }else{
                if(is_yesterday($sign->last_signin_time))
                    return $this->apiReturn(true,'success',['score'=>$sign->score,'days'=>$sign->days,'status'=>0]);
                else
                    return $this->apiReturn(true,'success',['score'=>$sign->score,'days'=>0,'status'=>0]);
            }
        }else{
            Sign::create(['wxuser_id'=>$this->auth()->id,'score'=>10]);
            return $this->apiReturn(true,'success',['score'=>10,'days'=>0,'status'=>0]);
        }
    }
    //签到赢积分
    public function sign(){
        $days = $this->auth()->signIn();
        Wxuser::where('id',$this->auth()->id)->increment('score',10);
        return $this->apiReturn(true,'success',['days'=>$days]);
    }
    //积分兑换
    public function exchange()
    {
        if (Input::has('goods_id')) {
            $id = Input::get('goods_id');
            $addressId = Input::get('address_id');
            $shop = Scoreshop::find($id);
            if ($shop) {
                $score = $this->auth()->score;
                if ($shop->need > $score) {
                    return $this->apiReturn(false, '积分不足');
                } else {
                    //记录积分明细
                    $info = [
                        'wxuser_id'=>$this->auth()->id,
                        'prize_id'=>$shop->id,
                        'title'=>$shop->title,
                        'coverimage'=>$shop->coverimage,
                        'spend'=>$shop->need,
                        'address_id'=>$addressId,
                        'status'=>0
                    ];
                    $list=Scorelist::create($info);
                    $list->status = 1;
                    $list->save();
                    //更新商品数量
                    if ($shop->amount > 0) {
                        $shop->amount = $shop->amount - 1;
                        $shop->save();
                    } else {
                        return $this->apiReturn(false, '商品数量不足');
                    }
                    //更新用户积分
                    $this->auth()->decrement('score', $shop->need);
                    return $this->apiReturn(true, '兑换成功', $this->auth()->score);
                }
            } else {
                return $this->apiReturn(false, '商品不存在');
            }
        }
    }
    //领取游记积分
    public function getScore(){
        $input = request();
        //更新用户积分
        $this->auth()->increment('score', $input->score);
        Travelnote::where('id',$input->id)->update(['sign' => 1]);
        return $this->apiReturn(true, '领取成功');
    }
    //获取积分奖品列表
    public function scoreShop(){
        $list = Scoreshop::all();
        editImageUrl($list);
        return $this->apiReturn(true, 'success',$list);
    }
    //记录积分明细
    public function scoreList(){
        $list = $this->auth()->scorelists()->simplePaginate(10);
        foreach ($list as $v){
            $v->addresses;
        }
        editImageUrl($list);
        getExpressInfo($list);
        return $this->apiReturn(true, 'success',$list);
    }
    //福利抽奖奖品列表
    public function prizeShop(){
        $list = Prize::all();
        editImageUrl($list);
        return $this->apiReturn(true, 'success',['list'=>$list,'score'=>config('lottery_score')]);
    }
    //记录抽奖明细
    public function prizeList(){
        $list = $this->auth()->prizelists()->simplePaginate(10);
        foreach ($list as $v){
            $v->addresses;
        }
        editImageUrl($list);
        getExpressInfo($list);
        return $this->apiReturn(true, 'success',$list);
    }
    //福利抽奖
    public function prize()
    {
        $spend = intval(Input::get('spend'));
        $item = 0;
        $prize_arr = [];
        $arr = [];
        try {
            //获取用户积分
            $score = $this->auth()->score;
            if ($spend > $score) {
                return $this->apiReturn(false, '积分不足');
            } else {
                //获取奖品数组
                $lists = Prize::all();
                foreach ($lists as $list) {
                    $id = $list['id'];//奖项编号
                    $title = $list['title'];//奖项标题
                    $amount = $list['amount'];//奖项数量
                    $prop = $list['prop'];//中奖概率
                    $prize_arr[$item] = array('id' => $id, 'title' => $title, 'amount' => $amount, 'prop' => $prop);
                    $item++;
                }
                foreach ($prize_arr as $key => $val) {
                    $arr[$key] = $val['prop'];
                }
                $rid = $this->getRand($arr, $prize_arr); //中奖结果

                $prizeid = $rid;
                $prizeInfo = $prize_arr[$rid]['title'];
                //更新个人积分
                $this->auth()->decrement('score', $spend);
                if($prizeid!=0){
                    //更新奖品数量
                    $prize=Prize::where('id',$prizeid+1);
                    $prize->decrement('amount');
                    $coverimage=$prize->first()->coverimage;
                    Prizelist::create(['title' => $prizeInfo, 'wxuser_id' => $this->auth()->id,'coverimage'=>$coverimage, 'spend' => $spend, 'prizeid' => $prizeid]);
                    return $this->apiReturn(true, 'success', ['prizeid'=>$prizeid,'prizeInfo'=>$prizeInfo]);
                }else{
                    return $this->apiReturn(true, 'success', ['prizeid'=>$prizeid,'prizeInfo'=>false]);
                }
            }
        } catch (MysqliException $e) {
            return $this->apiReturn(false, 'error', $e->getMessage());
        }
    }
    //获取奖项
    function getRand($proArr, $prize_arr)
    {
        $result = 0;
        $proSum = array_sum($proArr);
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur && $prize_arr[$key]['amount'] > 0) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }
    //福利抽奖奖品兑换
    public function prizeExchange(){
        $prize_id  = Input::get('prize_id');
        $addressId = Input::get('address_id');
        //记录积分明细
        $info = [
            'address_id'=>$addressId,
            'status'=>1
        ];
        Prizelist::find($prize_id)->update($info);
        return $this->apiReturn(true, '兑换成功');
    }
}